﻿using KPIS.GERP.GAMS.APP.App_UI.Commons;
using KPIS.GERP.GAMS.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ReportRemainExpense : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_BGM_MAS_Source source = new D_BGM_MAS_Source();
        D_ACM_MiscReport rpt = new D_ACM_MiscReport();
        MailMerge mailMerge = new MailMerge();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDateStart.SetValue(DateTime.Today);
                LoadDDLBgmSourceSeq();
            }
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = source.GetList();
            ddlBgmSourceSeq.DataBind();

            main.InsertEmptyDDLAll(ddlBgmSourceSeq);
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            string strReportHeader = "";
            string strReportTitle = "";
            string strEventSeq = "";
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            DateTime? dtStartDate = txtDateStart.GetDateValue();

            if (!dtStartDate.HasValue)
            {
                main.MessageAlert(Page, "กรุณาระบุวันที่");
                return;
            }

            switch (rblEventSeq.SelectedValue)
            {
                case "11":
                    strReportTitle = "เจ้าหนี้การค้าคงค้าง ";
                    strEventSeq = "11";
                    break;
                case "12":
                    strReportTitle = "ใบสำคัญค้างจ่าย ";
                    strEventSeq = "12, 14";
                    break;
                case "13":
                    strReportTitle = "เจ้าหนี้อื่นคงค้าง ";
                    strEventSeq = "";
                    break;
                default:
                    break;
            }

            DataTable dtb = null;
            
            if (string.IsNullOrEmpty(strEventSeq))
            {
                dtb = rpt.ReportRemainCreditor(strBgmSourceSeq, dtStartDate.Value);
            }
            else
            {
                dtb = rpt.ReportRemainExpense(strBgmSourceSeq, strEventSeq, dtStartDate.Value);
            }

            Dictionary<string, DataTable> dic = new Dictionary<string, DataTable>();
            dic.Add("A6", dtb);

            Dictionary<string, string> dic2 = new Dictionary<string, string>();
            strReportHeader = string.Format("ณ วันที่ {0}", dtStartDate.Value.ToLongDateString());
            dic2.Add("A1", DateTime.Today.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
            dic2.Add("A4", strReportHeader);
            dic2.Add("A3", strReportTitle + (ddlBgmSourceSeq.SelectedValue.Length > 0 ? ddlBgmSourceSeq.SelectedItem.Text : ""));

            double dAmount = 0;

            foreach (DataRow dr in dtb.Rows)
            {
                dAmount += main.ConvertNullDouble(dr["amount"]) ?? 0;
            }

            int nRow = dtb.Rows.Count + 6;
            dic2.Add("C" + nRow, "รวม");
            dic2.Add("D" + nRow, dAmount.ToString("#,##0.00"));

            mailMerge.MergeExcel(Response, Server.MapPath("~/Reports/Templates/RemainExpense.xlsx"), string.Format("RemainExpense-{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")), dic, dic2);
        }
    }
}